<template>
	<view class="login">
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="phoneData" type="number" maxlength="11" placeholder="用户名"></wInput>
				<wInput v-model="passData" type="password" maxlength="11" placeholder="密码" isShowPass></wInput>
			</view>
			<wButton text="登 录" :rotate="isRotate" @click.native="startLogin()"></wButton>

			<!-- 其他登录 -->
			<view class="other_login cuIcon">
				<view class="login_icon">
					<view class="cuIcon-weixin" @tap="login_weixin"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-weibo" @tap="login_weibo"></view>
				</view>
				<view class="login_icon">
					<view class="cuIcon-github" @tap="login_github"></view>
				</view>
			</view>

			<!-- 底部信息 -->
			<view class="footer">
				<navigator url="forget" open-type="navigate">找回密码</navigator>
				<text>|</text>
				<navigator url="register" open-type="navigate">注册账号</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button

	export default {
		data() {
			return {
				//logo图片 base64
				logoImage: '../../static/logo.png',
				phoneData: '18818429683', //用户/电话
				passData: '123456', //密码
				isRotate: false, //是否加载旋转
			};
		},
		components: {
			wInput,
			wButton,
		},
		mounted() {
			_this = this;
		},
		methods: {
			isLogin() {
				//判断缓存中是否登录过，直接登录
				try {
					const value = uni.getStorageSync('setUserData');
					if (value) {
						//有登录信息
						console.log("已登录用户：", value);
						_this.$store.dispatch("setUserData", value); //存入状态
						uni.reLaunch({
							url: '../../pages/index',
						});
					}
				} catch (e) {
					// error
				}
			},
			startLogin() {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.phoneData.length == "") {
					_this.$Common.toast("用户名不能为空");
					return;
				}
				if (this.passData.length == "") {
					_this.$Common.toast("密码不能为空");
					return;
				}
				_this.isRotate = true

				let clientInfo = _this.$Common.getClientIdAndOS();

				var sendData = {
					Phone: this.phoneData,
					MM: this.passData,
					ClientId: clientInfo.ClientId,
					MobileType: clientInfo.MobileType
				};
				console.log(JSON.stringify(sendData));

				_this.$Common.RUNTIME.ShowLoading = false;
				_this.$http.get('CheckLogin', sendData).then(res => {
					_this.isRotate = false
					let reData = res.length > 0 ? res[0] : {};
					console.log(JSON.stringify(reData));
					_this.$Common.toast("登录成功");
					try {
						uni.setStorageSync('USERINFO', JSON.stringify(reData));
					} catch (e) {
						// error
					}
				}).catch(res => {
					if (res.Code == 201)
						_this.$Common.toast("用户名或密码不正确");
					else if (res.Message)
						_this.$Common.toast(res.Message);
					else
						_this.$Common.toast("登录出错，请重试...");

					_this.isRotate = false
				})
			},
			login_weixin() {
				//微信登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_weibo() {
				//微博登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			},
			login_github() {
				//github登录
				uni.showToast({
					icon: 'none',
					position: 'bottom',
					title: '...'
				});
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");
</style>
